package com.kun.product.service.impl;

import com.kun.product.dao.ClassifyDao;
import com.kun.product.entity.ClassifyEntity;
import com.kun.product.vo.ProductClassifyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.product.dao.ProductDao;
import com.kun.product.entity.ProductEntity;
import com.kun.product.service.ProductService;
import org.springframework.util.StringUtils;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private ClassifyDao classifyDao;
    
    
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        String key = (String) params.get("key");
        String cid = (String) params.get("cid");
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cid)){
            queryWrapper.eq("cid",cid);
        }
        String productName= (String) params.get("productName");
        if (!StringUtils.isEmpty(productName)){
            queryWrapper.like("product_name",productName);
        }


        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<ProductEntity> records = page.getRecords();
        List<ProductClassifyVo> productClassifyVos = records.stream().map((productEntity) -> {
            ProductClassifyVo productClassifyVo = new ProductClassifyVo();
            BeanUtils.copyProperties(productEntity, productClassifyVo);
            ClassifyEntity id = classifyDao.selectOne(new QueryWrapper<ClassifyEntity>().eq("id", productEntity.getCid()));
            productClassifyVo.setClassify(id.getClassify());
            return productClassifyVo;
        }).collect(Collectors.toList());
        pageUtils.setList(productClassifyVos);
        return pageUtils;

    }

}